package com.smallpineapple.turing.service.admin.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.smallpineapple.turing.entity.Member;
import com.smallpineapple.turing.entity.Teacher;
import com.smallpineapple.turing.service.admin.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Zeng
 * @date 2020/2/15 22:45
 */
@Service
public class TokenServiceImpl implements TokenService {

    private final Integer EXPIRE_TIME = 1*60*60*1000;

    @Override
    public String getTeacherToken(Teacher teacher) {
        String token="";

        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        token= JWT.create().withAudience(String.valueOf(teacher.getTeacherId()))
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(teacher.getTeacherPassword()));
        return token;
    }

    @Override
    public String getMemberToken(Member member) {
        String token="";
        token= JWT.create().withAudience(String.valueOf(member.getMemberId()))
                .sign(Algorithm.HMAC256(member.getPassword()));
        return token;
    }
}
